import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

import ljb from './ljb-router'
import pj from './pj-router'
import zl from './zl-router'

import Main from '../views/Main.vue'
import Home from '../views/Home.vue'
import FindMedicine from '../views/FindMedicine.vue'
import MedicineCard from '../views/MedicineCard.vue'
import Cart from '../views/Cart.vue'
import Mine from '../views/Mine.vue'

Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    component: Main,
    children: [
      {
        path: '/',
        component: Home
      },
      {
        path: '/find',
        component: FindMedicine
      },
      {
        path: '/card',
        component: MedicineCard
      },
      {
        path: '/cart',
        component: Cart
      },
      {
        path: '/mine',
        component: Mine
      },
    ]
  },

  ...ljb,
  ...pj,
  ...zl
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  var path = to.path;
  if (path.indexOf('/mine') != -1 || path.indexOf('/cart') != -1 || path.indexOf('/check') != -1) {
    if (store.state.zlstore.userinfo) { //判断用户是否登录
      next();
    } else {
      next({ path: '/children/login', query: { target: path } });  //引导用户登录
    }
  } else {
    next()
  }
})

export default router
